<style>
.error{
	color:red;
}
</style>
<?php	
$empty = 'You can\'t leave this empty';
if(isset($_POST['submit'])){
	$warn = false;
	$qfNameError='';
	$deadlineError = '';
	if($_POST['qfName'] == ''){
		$warn = true;
		$qfNameError = $empty;
	}
	$questionError = array();
	$choiceError = array();
	foreach($_POST['question'] as $key => $value){
		$value = trim($value);
		if($value == ''){
			$warn = true;
			$questionError[$key] = $empty;
		}
	}
	if(isset($_POST['choice'])){
		foreach($_POST['choice'] as $key => $value){
			$value = trim($value);
			if($value == ''){
				$warn = true;
				$choiceError[$key] = $empty;
			}
		}
	}
	if(session_id() == '')
		session_start();
	if(isset($_POST['deadlineEnable'])){
		if($_POST['year'] == '' || $_POST['day'] == ''){
			$warn = true;
			$deadlineError = $empty;
		}
	}
	
	if(!$warn){		
		$uid = 0;
		if(isset($_SESSION['uid']))
			$uid = $_SESSION['uid'];
		$password = '';
		if(isset($_POST['password']))
			$password = $_POST['password'];
		$name = $_POST['qfName'];
		$name = str_replace('\'','\\\'',$name);
		$timestamp = 0;
		if(isset($_POST['year'])){
			$year = $_POST['year'];
			$month = $_POST['month'];
			$day = $_POST['day'];
			$timestamp = strtotime("$day-$month-$year");
		}	
		require('conn.php');
		
		mysql_query("insert into questionForm(qfid,uid,password,deadline,name) values('',$uid,'$password','$timestamp','$name')");	
		$qfid = mysql_insert_id();
		$pointer = 0;
		foreach($_POST['question'] as $key => $value){
			$value = str_replace('\'','\\\'',$value);
			$type = 1;
			if($_POST['choiceCount'][$key] == 0)
				$type = 0;
			
			mysql_query("insert into question(qid,qfid,question,type) values('',$qfid,'$value',$type)");
			$qid = mysql_insert_id();
			if($_POST['choiceCount'][$key] > 0){
				for($i = $pointer; $i < $pointer+$_POST['choiceCount'][$key];$i++){
					$choice = $_POST['choice'][$i];
					mysql_query("insert into mc(choice,qid,vote) values('$choice',$qid,0)");
				}
				$pointer+=$_POST['choiceCount'][$key];
			}
		}
		mysql_close($connect);
		header('location:confirm.php?id='.$qfid);
	}
	else{
		if(isset($_POST['deadlineEnable'])){
			$_SESSION['deadlineError'] = $deadlineError;
			$_SESSION['year'] = $_POST['year'];
			$_SESSION['month'] = $_POST['month'];
			$_SESSION['day'] = $_POST['day'];
		}
		$_SESSION['qfName'] = $_POST['qfName'];
		$_SESSION['qfNameError'] = $qfNameError;
		$_SESSION['questionError'] = $questionError;
		$_SESSION['choiceError'] = $choiceError;
		$_SESSION['choiceCount'] = $_POST['choiceCount'];
		$_SESSION['question'] = $_POST['question'];
		$_SESSION['qTypes'] = $_POST['qTypes'];
		if(isset($_POST['choice']))
			$_SESSION['choice'] = $_POST['choice'];		 
	}		
}

include_once("header.php");
$qfName = '';
$qfNameError = '';
if(isset($_SESSION['choiceCount'])){
	$qfName = $_SESSION['qfName'];
	$qfNameError = $_SESSION['qfNameError'];
	unset($_SESSION['qfName']);
	unset($_SESSION['qfNameError']);
	$questionError = $_SESSION['questionError'];
	$choiceError = $_SESSION['choiceError'];
	$choiceCount = $_SESSION['choiceCount'];
	$question = $_SESSION['question'];
	$qTypes = $_SESSION['qTypes'];
	unset($_SESSION['questionError']);
	unset($_SESSION['choiceError']);
	unset($_SESSION['choiceCount']);
	unset($_SESSION['question']);
	unset($_SESSION['qTypes']);
	if(isset($_SESSION['choice'])){
		$choice = $_SESSION['choice'];
		unset($_SESSION['choice']);
	}
}
if(isset($_SESSION['deadlineError'])){
	$year = $_SESSION['year'];
	$month = $_SESSION['month'];
	$day = $_SESSION['day'];
	$deadlineErr = $_SESSION['deadlineError'];
	unset($_SESSION['year']);
	unset($_SESSION['month']);
	unset($_SESSION['day']);
	unset($_SESSION['deadlineError']);
}


?>
<link type="text/css" rel="stylesheet" href="css/question_form.css" />
<script src="js/jquery-1.11.0.js"></script>
<script src="js/question_form.js"></script>

<form action="" method="post">


<div class="table">
	<div class="tr">
		<div>Name of Question Form:</div>
		<div><input type="text" name="qfName" size="30" value="<?php echo $qfName;?>"></div>
		<div class="error"><?php echo $qfNameError?></div>
		<br>
	</div>
	<div class="tr">
		<div class="td" id="allQuestionBlock">
<?php 
			if(isset($questionError)){
				$pointer = 0;
				foreach($question as $key => $value){
	
?>
					<div>
						<input type="hidden" name="choiceCount[]" value="<?php echo $choiceCount[$key];?>">
						<div>
							<select name="qTypes[]" style="width:100px">
								<option value="open">Open</option>
<?php 
								$select = '';
								if($qTypes[$key] == 'mc')
									$select = 'selected="selected"';
?>
								<option value="mc" <?php echo $select;?>>MC</option>
							</select>
							<input type="text" class="question" name="question[]" placeholder="Enter your question here" value="<?php echo $question[$key]; ?>" size="50">
							<button type="button" class="minusQuestion" style="width:30px">-</button>			
						</div>
						<div class="error">
<?php
						if(isset($questionError[$key]))
							echo $questionError[$key];
?>
						</div>
<?php 
						if($choiceCount[$key] != 0){
?>
							<div class="table">
								<div class="tr">
									<div class="td">
<?php 
										for($i = 0; $i < $choiceCount[$key]; $i++){
?>

											<div>
												<div>
													<input type="text" class="choice" name="choice[]" placeholder="Enter content of choice" value="<?php echo $choice[$pointer+$i];?>" size="30">
													<button type="button" class="minusChoice" style="width:30px">-</button>
												</div>
												<div class="error">
<?php
													if(isset($choiceError[$key])) 
														echo $choiceError[$key];
?>
												</div>
																			
											</div>
<?php 
										}
										$pointer+=$choiceCount[$key];
?>
									</div>
									<div class="td">
										<button type="button" class="addChoice" style="margin-left: 30px">+</button>
									</div>
								</div>
							</div>
<?php
						} 	
?>	
					</div>
					<br>
<?php
				}
			}
			else{
?>			
				<div>
					<input type="hidden" name="choiceCount[]" value="0">
					<div>
						<select name="qTypes[]" style="width:100px">
						<option value="open">Open</option>
						<option value="mc">MC</option>
						</select>
						<input type="text" class="question" name="question[]" placeholder="Enter your question here" size="50">
						<button type="button" class="minusQuestion" style="width:30px">-</button>
					</div>	
					<div class="error"></div>						
				</div>
				<br>
<?php
			}
?>
		</div>
		<div class="td">
			<button type="button" id="addQuestion" style="margin-left: 30px;width:30px">+</button>
		</div>
	</div>
	<div class="tr">
<?php
	if(!isset($deadlineErr)){
?>
		<input type="checkbox" id="deadlineEnable" name="deadlineEnable" value="true"> Enable a deadline for the form<br>
		<div id="deadlineSetting" style="display: none";>
			<input type="text" name="year" placeholder="yyyy" size="4">
			<select name="month">
<?php
			for($i = 1; $i <= 12; $i++){
?>
				<option value="<?php echo $i?>"><?php echo $i?></option>
<?php
			}
?>
			</select>
			<input type="text" name="day" placeholder="dd" size="2">
		</div>
<?php
	}
	else{
?>
		<input type="checkbox" id="deadlineEnable" name="deadlineEnable" value="true" checked="checked"> Enable a deadline for the form<br>
		<div id="deadlineSetting">
			<input type="text" name="year" placeholder="yyyy" size="4" value="<?php echo $year;?>">
			<select name="month">
<?php
			for($i = 1; $i <= 12; $i++){
				if($month == $i){
?>
					<option value="<?php echo $i?>" selected="selected"><?php echo $i?></option>
<?php
				}
				else{
?>
					<option value="<?php echo $i?>"><?php echo $i?></option>
<?php
				}
			}
?>
			</select>
			<input type="text" name="day" placeholder="dd" size="2" value="<?php echo $day;?>">
		</div>
<?php
	}
?>
		<div class="error" id="deadlineError">
<?php 
			if(isset($deadlineErr))
				echo $deadlineErr;
?>
		</div>
		<br>
	</div>
	<div class="tr">
		<input type="checkbox" id="passwordEnable" name="passwordEnable" value="true"> Enable a password for the form<br>
		<div id="passwordSetting" style="display: none"><input type="password" name="password" placeholder="Enter your password of the form here" size="50"></div>
		<br>
	</div>
	<div class="tr">
		<input type="checkbox" id="groupsEnable" name="groupsEnable" value="true" disabled=disabled> <font style="text-decoration:line-through">Enable group(s) to the form (For all MC Question(s))</font><br>
		<div id="groupsSetting" style="display: none">
			<div class="table">
				<div class="tr">
					<div class="td">
						<div class="group">
							<input type="text" name="group[]" placeholder="group name" size="15">
							<input type="text" name="groupPassword[]" style="margin-left: 15px" placeholder="password" size="15">
							<input type="text" name="voteRate[]" placeholder="1:?vote(s)" style="margin-left: 15px" size="8">
							<button type="button" class="minusGroup" style="width:30px">-</button>
						</div>
					</div>
					<div class="td">
						<button type="button" id="addGroup" style="margin-left: 30px">+</button>
					</div>
				</div>
			</div>
					
			
		</div>
		<br>
	</div>
</div>
<input type="submit" name="submit" value="Create">
</form>
